/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logical.allhit.gui;

import com.logical.allhit.setting.Proxy;
import com.logical.allhit.setting.Setting;
import com.logical.allhit.util.ScrollUtil;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author comp18
 */
public class ProxyList extends javax.swing.JDialog {

    /**
     * Creates new form ProxyList
     */
    public ProxyList(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        proxyTable.setModel(new ProxyTableModel());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        proxyTable = new javax.swing.JTable();
        addButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        helpButton = new javax.swing.JButton();

        jLabel1.setText("jLabel1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Proxy List");

        proxyTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(proxyTable);

        addButton.setText("Add Proxy");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        removeButton.setText("Remove");
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        helpButton.setText("Help");
        helpButton.setToolTipText("");
        helpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(addButton, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                    .addComponent(removeButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(helpButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(49, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(helpButton)
                        .addGap(0, 90, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        ProxyTableModel model = (ProxyTableModel) proxyTable.getModel();
        model.addRow(new Proxy());
        ScrollUtil.scroll(proxyTable, ScrollUtil.LEFT, ScrollUtil.BOTTOM);
    }//GEN-LAST:event_addButtonActionPerformed

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
        ProxyTableModel model = (ProxyTableModel) proxyTable.getModel();
        int[] selected = proxyTable.getSelectedRows();
        for(int i : selected){
            model.remove(i);
        }
        ScrollUtil.scroll(proxyTable, ScrollUtil.LEFT, ScrollUtil.BOTTOM);
    }//GEN-LAST:event_removeButtonActionPerformed

    private void helpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpButtonActionPerformed
        try {
            JOptionPane.showMessageDialog(MainView.getInstance().getFram(), "Use http://hidemyass.com/proxy-list for list");
        } catch (Exception e) {
        }
    }//GEN-LAST:event_helpButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JButton helpButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable proxyTable;
    private javax.swing.JButton removeButton;
    // End of variables declaration//GEN-END:variables
    static class ProxyTableModel extends AbstractTableModel {

        ArrayList<Proxy> proxys;
        // These are the names of the columns represented by this TableModel
        static final String[] columnNames = new String[]{"Host", "Port"};
        // These are the types of the columns represented by this TableModel
        static final Class[] columnTypes = new Class[]{String.class, Integer.class};

        public ProxyTableModel() {
            proxys = Setting.getInstance().getProxys();
        }

        @Override
        public int getRowCount() {
            return proxys.size();
        }

        @Override
        public int getColumnCount() {
            return columnNames.length;
        }

        @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }

        @Override
        public Class getColumnClass(int column) {
            return columnTypes[column];
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Proxy proxy = proxys.get(rowIndex);
            switch (columnIndex) {
                case 0:
                    return proxy.getName();
                case 1:
                    return proxy.getPort();
                default:
                    return new Object();
            }
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return true;
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            Proxy proxy = proxys.get(rowIndex);
            switch (columnIndex) {
                case 0:
                    proxy.setName((String) aValue);
                    break;
                case 1:
                    proxy.setPort((Integer) aValue);
                    break;
            }
        }

        public boolean addRow(Proxy account) {
            if (proxys.add(account)) {
                fireTableDataChanged();
                return true;
            } else {
                return false;
            }
        }

        public boolean remove(int i) {
            proxys.remove(i);
            fireTableDataChanged();
            return true;
        }
    }
}
